﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 护士工作站
{
    public partial class 退药申请 : Form
    {
        public 退药申请()
        {
            InitializeComponent();
        }

        public DataTable dt = new DataTable();


      
        private void tb_ZYNo_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                string ZYNo = tb_ZYNo.Text.Trim();


                if (ZYNo == "")
                {
                    MessageBox.Show("请输入要查询的内容！");
                    tb_ZYNo.Focus();

                    return;
                }
                string Hos = "select * from InHospital,Patient,Bed where InHospital.HosNo='" + ZYNo + "'and InHospital.HosNo=Patient.HosNo and InHospital.BedNo=Bed.BedNo";
                using (SqlConnection conn = new SqlConnection(DBHelper.connString))
                {

                    try
                    {
                        conn.Open();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    SqlCommand cmd = new SqlCommand(Hos, conn);


                    if (ZYNo != "")
                    {
                        SqlDataReader dr = cmd.ExecuteReader();

                        if (dr.HasRows)
                        {
                            dr.Read();



                            tb_ks.Text = dr["HosDepartment"].ToString();
                            tb_fb.Text = dr["PatType"].ToString();
                            tb_xm.Text = dr["PatName"].ToString();
                            tb_ch.Text = dr["BedNo"].ToString();
                            tb_yjje.Text = dr["HosBalance"].ToString();



                        }
                        dr.Close();
                    }
                }
            }
        }

        private void tb_bh_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                string ZYNo = tb_ZYNo.Text.Trim();
                string bh = tb_bh.Text.Trim();

                if (bh  == "")
                {
                    MessageBox.Show("请输入要查询的内容！");
                    tb_bh.Focus();

                    return;
                }
                string WarContent = "select WarContent,MedDanwei,Medprice,MedModel,MedAmount,Medscalar ,AmountPaid from ConsumptionSum where HosNo='" + ZYNo + "'and WarNo='" + bh + "'and RefundCondition='已付款'";
                using (SqlConnection conn = new SqlConnection(DBHelper.connString))
                {
                    
                 
                    try
                    {
                        conn.Open();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    } 
                  SqlCommand comContent = new SqlCommand(WarContent, conn);
                    SqlDataAdapter da = new SqlDataAdapter();
                    DataSet ds = new DataSet();
                    da = new SqlDataAdapter(WarContent, conn);
                    ds = new DataSet();
                    da.Fill(ds);
                    
                    dataGridView2.DataSource = ds.Tables[0];
                    
                
                    } 
                        
                    }
                }
        

        private void button_tc_Click(object sender, EventArgs e)
        {
            this.Hide();
            
        }

        private void button_bc_Click(object sender, EventArgs e)
        {
            string ZYNo = tb_ZYNo.Text.Trim();
            string bh = tb_bh.Text.Trim();
            int Rcount = dataGridView2.Rows.Count-1;
            using (SqlConnection conn = new SqlConnection(DBHelper.connString))
            {  
                conn.Open();
                DateTime dt = System.DateTime.Now;
                string abc = string.Format("update Warn set WarnCondition='申退' where  HosNo='" + ZYNo + "'and WarnNo='" + bh + "'");
               string sql = string.Format("update ConsumptionSum set RefundCondition='申请退款'where  HosNo='" + ZYNo + "'and WarNo='" + bh + "'");
               string date = string.Format("update ConsumptionSum set ApplicationDate='"+dt +"'where  HosNo='" + ZYNo + "'and WarNo='" + bh + "'");
                SqlCommand com = new SqlCommand(abc ,conn );
               SqlCommand cam = new SqlCommand(sql , conn);
               SqlCommand Date = new SqlCommand(date, conn);
                int n = Convert.ToInt32(com.ExecuteNonQuery());
               int m = Convert.ToInt32(cam.ExecuteNonQuery());
               int z = Convert.ToInt32(Date.ExecuteNonQuery());
               if (n == Rcount && m == Rcount && z == Rcount)
                    MessageBox.Show("申退成功！");
                else
                    MessageBox.Show("申退失败！");

            }
        }

        private void button_qp_Click(object sender, EventArgs e)
        {
            tb_ks.Text = "";
            tb_fb.Text = "";
            tb_xm.Text = "";
            tb_ch.Text = "";
            tb_yjje.Text = "";
            tb_ZYNo.Text = "";
            tb_bh.Text = "";
            tb_fs.Text = "";

            DataTable dt = (DataTable)dataGridView2.DataSource;
            dt.Rows.Clear();
            dataGridView2.DataSource = dt;
          
        }

       

        
        }

      
    }
       
    

